package com.example.admin.brainwave;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.example.admin.brainwave.dao.DaoMaster;
import com.example.admin.brainwave.dao.DaoSession;
import com.example.admin.brainwave.utils.OkHttpUtil;
import com.example.admin.brainwave.utils.languageutils.MultiLanguageUtil;

public class MyApp extends Application {


    private static MyApp instance;

    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        OkHttpUtil.initContext(this);
        MultiLanguageUtil.init(this);
        setMyDataBase();
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    public static Context getMyApplication() {
        return instance;
    }

    private void setMyDataBase() {
        //创建数据库topnews.db"
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "topnews.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoSession() {
        return daoSession;
    }

}
